package com.woniu.adapter;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.outlet.dao.InoutOutPoMapper;
import com.woniu.outlet.dao.InventoryInoutPoMapper;
import com.woniu.outlet.dao.MaterialOutApplyPoMapper;
import com.woniu.outlet.po.*;
import com.woniu.util.SnowflakeIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Component
public class OutApplyAdapter {
    @Autowired
    private InventoryInoutPoMapper inventoryInoutPoMapper;

    @Autowired
    private MaterialOutApplyPoMapper materialOutApplyPoMapper;

    @Autowired
    private InoutOutPoMapper inoutOutPoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    private ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 查询所有退料单
     * @return
     */
    public  List<MaterialOutApplyPo> findMaterialOutApplyPos() {
        BoundValueOperations<String,String> opr = redisTemplate.boundValueOps("materialOutApplyPos");
        String s = opr.get();
        try {
            if(StringUtils.isEmpty(s)){
                List<MaterialOutApplyPo> materialOutApplyPos = materialOutApplyPoMapper.selectByExample(null);
                opr.set(objectMapper.writeValueAsString(materialOutApplyPos));
                return materialOutApplyPos;
            }else {
                return objectMapper.readValue(s, new TypeReference<List<MaterialOutApplyPo>>() {});
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 创建退货单
     * @param ids 需要退货的原料inout主键字符串
     * @param reason 退货理由
     * @param desc 退货描述
     * @param workerId 创建退货单的员工id
     */
    public void addMaterialOutApply(String ids,String reason,String desc,Integer workerId){
        if(StringUtils.isEmpty(ids)){
            return;
        }
        MaterialOutApplyPo materialOutApplyPo = new MaterialOutApplyPo();
        materialOutApplyPo.setCreateTime(new Date());
        materialOutApplyPo.setCreateWorkerId(workerId);
        materialOutApplyPo.setReason(reason);
        materialOutApplyPo.setDecs(desc);
        materialOutApplyPo.setStatus("N");

        SnowflakeIdGenerator idWorker = new SnowflakeIdGenerator();
        long startTime = System.nanoTime();
        long id = idWorker.nextId();
        materialOutApplyPo.setCode("OUT"+id);

        materialOutApplyPoMapper.insertSelective(materialOutApplyPo);


        String[] inventoryInOutIds = ids.split(",");

        for (String inventoryInOutId : inventoryInOutIds) {
            int i = Integer.parseInt(inventoryInOutId);
            InventoryInoutPo inventoryInoutPo = new InventoryInoutPo();
            inventoryInoutPo.setIsout("N");
            inventoryInoutPo.setId(i);
            inventoryInoutPoMapper.updateByPrimaryKeySelective(inventoryInoutPo);

            InoutOutPo inoutOutPo = new InoutOutPo();
            inoutOutPo.setOutApplyId(materialOutApplyPo.getId());
            inoutOutPo.setInventoryInoutId(i);
            inoutOutPoMapper.insertSelective(inoutOutPo);
        }

        redisTemplate.delete("materialOutApplyPos");
    }

    /**
     * 1.修改退单申请的状态
     * 2.修改出入库状态
     * @param id 退单申请id
     */
    public void updateOutApply(Integer id){
        MaterialOutApplyPo materialOutApplyPo = new MaterialOutApplyPo();
        materialOutApplyPo.setStatus("Y");
        materialOutApplyPo.setId(id);
        materialOutApplyPoMapper.updateByPrimaryKeySelective(materialOutApplyPo);

        InoutOutPoExample inoutOutPoExample = new InoutOutPoExample();
        InoutOutPoExample.Criteria criteria = inoutOutPoExample.createCriteria();
        criteria.andOutApplyIdEqualTo(id);
        List<InoutOutPo> inoutOutPos = inoutOutPoMapper.selectByExample(inoutOutPoExample);
        for (InoutOutPo inoutOutPo : inoutOutPos) {
            InventoryInoutPo inventoryInoutPo = new InventoryInoutPo();
            inventoryInoutPo.setId(inoutOutPo.getInventoryInoutId());
            inventoryInoutPo.setIsout("Y");
            inventoryInoutPoMapper.updateByPrimaryKeySelective(inventoryInoutPo);
        }
    }


}
